import os


def get_file_size(file_path):
    # 获取文件大小，以字节为单位
    size = os.path.getsize(file_path)
    return size

def compare_file_size(file1, file2):
    size1 = get_file_size(file1)
    size2 = get_file_size(file2)

    if size1 > size2:
        return file1
    elif size2 > size1:
        return file2
    else:
        return "Both files have the same size."

def compare_txt_files(file_a, file_b):
    with open(file_a, 'r') as f1, open(file_b, 'r') as f2:
        return f1.read() == f2.read()



def compare_modification_time(file1, file2):
    # 获取文件的修改时间戳
    mtime1 = os.path.getmtime(file1)
    mtime2 = os.path.getmtime(file2)

    # 如果第二个文件的修改时间早于第一个，返回True
    return mtime2 < mtime1


def validate_and_remove_files(folder_a, folder_b):
    files_a = set(os.listdir(folder_a))
    files_b = set(os.listdir(folder_b))

    # 找出文件夹A中存在但文件夹B中不存在的文件
    files_not_in_b = files_a - files_b

    if files_not_in_b:
        print("Files in folder A that do not exist in folder B:")
        # for file in files_not_in_b:
        #     print(file)

    else:
        print("All files in folder A exist in folder B.")

    cnt_txt = 0
    cnt = 0

    # 找出文件夹B中存在但文件夹A中也存在的txt文件，验证内容是否相同，然后移除这些txt文件
    for file in files_a.intersection(files_b):
        file_a = os.path.join(folder_a, file)
        file_b = os.path.join(folder_b, file)

        if os.path.exists(file_b):
            if file.endswith(".txt") or file.endswith(".json"):
                if compare_txt_files(file_a, file_b):
                # if True:
                    os.remove(file_b)
                    cnt_txt += 1
                    print(f"Removed from folder B as content is the same {file_b}")
                else:
                    if compare_modification_time(file_a, file_b):
                        os.remove(file_b)
                        cnt_txt += 1
                        print(f"Removed from folder B as content is the same {file_b}")
                    else:
                        pass
                        # print(f"not same {file} from folder B as content.")
                    pass
                    # print(f"not same {file} from folder B as content.")
            elif file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png"):
                # os.remove(file_b)
                # cnt += 1
                size1 = get_file_size(file_a)
                size2 = get_file_size(file_b)
                if size1 == size2:
                # if True:
                    os.remove(file_b)
                    cnt += 1
                    print(f"Removed from folder B as file name is the same. {file_b}")
                else:
                    # if size1 > size2:
                    #     # if True:
                    #     os.remove(file_b)
                    #     cnt += 1
                    #     print(f"Removed from folder B as file name is the same. {file_b}")
                    # else:
                    #     print(f"not size {file} from folder B as content.")
                    # print(f"not size {file} from folder B as content.")
                    pass
    print(f"cnt_txt: {cnt_txt} cnt: {cnt}")


# 指定要验证的两个文件夹路径
folder_a = "/home/champrin/Desktop/record_data/网盘/兑换框数据集/2024-兑换框/5.15all (3)/images"
folder_b = "/home/champrin/Desktop/record_data/网盘/兑换框数据集/2024-5.15-自家/5.15all"






# 执行验证
validate_and_remove_files(folder_a, folder_b)

# files_a = set(os.listdir(folder_a))
# files_b = set(os.listdir(folder_b))
#
# # 找出文件夹A中存在但文件夹B中不存在的文件
# files_not_in_b = files_a - files_b
#
# print(files_a & files_b)
#
# if files_not_in_b:
#     print("Files in folder A that do not exist in folder B:")
#     # for file in files_not_in_b:
#     #     print(file)
# else:
#     print("All files in folder A exist in folder B.")

# file1 = "/home/champrin/Desktop/record_data/网盘/兵种内录/装甲板数据集/amaor/Data_big/check/all/Data/BigN113.txt"
# file2 = "/home/champrin/Desktop/record_data/网盘/兵种内录/装甲板数据集/amaor/Data_big/check/黄栋/Data_val/BigN113.txt"
#
# result = compare_modification_time(file1, file2)
# print(result)  # 如果第二个文件修改时间早于第一个，则输出True，否则输出False